package com.ztesoft.ossup.report.dao;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.util.StringUtils;

import com.ztesoft.ossup.report.domain.ReportChart;
import com.ztesoft.ossup.report.domain.ReportChart_;

/**
 */
public interface ChartRepository extends JpaRepository<ReportChart, Long>, JpaSpecificationExecutor<ReportChart> {

    class ChartSpecifications {

        public static Specification<ReportChart> likeChartName(final String name) {
            return new Specification<ReportChart>() {
				@Override
				public Predicate toPredicate(Root<ReportChart> root, CriteriaQuery<?> criteriaQuery,
						CriteriaBuilder criteriaBuilder) {
						    if (!StringUtils.isEmpty(name))
						        return criteriaBuilder.like(root.get(ReportChart_.title), "%" + name + "%");
						    return null;
						}
			};
        }
    }
    
    ReportChart findByReportId(Long reportId);
    
    void deleteByReportId(Long reportId);
}
